package com.qike.duola.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qike.duola.domain.*;
import com.qike.duola.dto.CarAddDto;
import com.qike.duola.dto.CarEditDto;
import com.qike.duola.dto.CarListDto;
import com.qike.duola.dto.DeptDelDto;
import com.qike.duola.enums.*;
import com.qike.duola.mapper.*;
import com.qike.duola.service.DuolaCarService;
import com.qike.duola.service.DuolaGoodsService;
import com.qike.duola.service.DuolaLineService;
import com.qike.duola.vo.*;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Eric.zhou
 * @Date: 2020/9/2 15:10
 */
@Service
public class DuolaLineServiceImpl implements DuolaLineService {
    private static final Logger logger = LoggerFactory.getLogger(DuolaLineServiceImpl.class);

    @Autowired
    private DuolaLineMapper duolaLineMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private DuolaLineTransMapper duolaLineTransMapper;
    @Autowired
    private DuolaCarMapper duolaCarMapper;
    @Autowired
    private DuolaCarRemainMapper duolaCarRemainMapper;


    /**
     * 生产线排队车辆列表
     * @return
     */
    @Override
    public List<LineListVo> lineList() {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        logger.info(userId+"生产线排队车辆列表");
        List<LineListVo> list = new ArrayList<>();

        DuolaLineExample duolaLineExample = new DuolaLineExample();
        duolaLineExample.createCriteria().andStatusEqualTo(StatusEnums.NO.getEcode());
        List<DuolaLine> duolaLines = duolaLineMapper.selectByExample(duolaLineExample);
        //遍历出所有车辆
        Map<Integer,DuolaCar> carMap = new HashMap<>();
        DuolaCarExample carExample = new DuolaCarExample();
        List<DuolaCar> duolaCars = duolaCarMapper.selectByExample(carExample);
        for (DuolaCar duolaCar : duolaCars) {
            carMap.put(duolaCar.getId(),duolaCar);
        }
        //遍历出所有车辆余料
        Map<Integer,DuolaCarRemain> carRemainHashMap = new HashMap<>();
        List<DuolaCarRemain> duolaCarRemains = duolaCarRemainMapper.selectByExample(new DuolaCarRemainExample());
        for (DuolaCarRemain duolaCarRemain : duolaCarRemains) {
            carRemainHashMap.put(duolaCarRemain.getCarId(),duolaCarRemain);
        }
        //查询中间表
        Map<Integer,List<LineCarVo>> duolaLineTransMap = new HashMap<>();
        DuolaLineTransExample lineTransExample = new DuolaLineTransExample();
        lineTransExample.createCriteria().andStatusEqualTo(StatusEnums.NO.getEcode());
        lineTransExample.setOrderByClause("sort asc");
        List<DuolaLineTrans> duolaLineTrans = duolaLineTransMapper.selectByExample(lineTransExample);
        for (DuolaLineTrans duolaLineTran : duolaLineTrans) {
            if (duolaLineTransMap.get(duolaLineTran.getLineId())==null){
                List<LineCarVo> lineTrans = new ArrayList<>();
                LineCarVo vo = new LineCarVo();
                vo.setId(duolaLineTran.getId());
                vo.setCarId(duolaLineTran.getCarId());
                vo.setCarNo(carMap.get(duolaLineTran.getCarId()).getCarNo());
                vo.setTransId(duolaLineTran.getTransId());
                vo.setSort(duolaLineTran.getSort());
                if (carRemainHashMap.get(duolaLineTran.getCarId())==null){
                    vo.setRemaining(new Double(0));
                }else {
                    vo.setRemaining(carRemainHashMap.get(duolaLineTran.getCarId()).getRemain());
                }
                lineTrans.add(vo);
                duolaLineTransMap.put(duolaLineTran.getLineId(),lineTrans);
            }else {
                List<LineCarVo> lineTrans = duolaLineTransMap.get(duolaLineTran.getLineId());
                LineCarVo vo = new LineCarVo();
                vo.setId(duolaLineTran.getId());
                vo.setCarId(duolaLineTran.getCarId());
                vo.setCarNo(carMap.get(duolaLineTran.getCarId()).getCarNo());
                vo.setTransId(duolaLineTran.getTransId());
                vo.setSort(duolaLineTran.getSort());
                if (carRemainHashMap.get(duolaLineTran.getCarId())==null){
                    vo.setRemaining(new Double(0));
                }else {
                    vo.setRemaining(carRemainHashMap.get(duolaLineTran.getCarId()).getRemain());
                }
                lineTrans.add(vo);
                duolaLineTransMap.put(duolaLineTran.getLineId(),lineTrans);
            }
        }
//        for (DuolaLineTrans duolaLineTran : duolaLineTrans) {
//            List<LineCarVo> voList = new ArrayList<>();
//        }

        //拼接数据
        for (DuolaLine duolaLine : duolaLines) {
            LineListVo vo = new LineListVo();
            vo.setId(duolaLine.getId());
            vo.setLineName(duolaLine.getLineName());
            vo.setList(duolaLineTransMap.get(duolaLine.getId()));
            list.add(vo);
        }
        return list;
    }


}
